using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;

namespace MasomRecieptBookApplication
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public partial class Form2 : System.Windows.Forms.Form
	{
		
		private System.Drawing.Printing.PrintDocument printDocument1;
		private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
		private System.Windows.Forms.Button button4;
		private System.Drawing.Printing.PrintDocument printDocument2;
		private System.Windows.Forms.PrintPreviewDialog printPreviewDialog2;
		private System.Windows.Forms.PrintDialog printDialog1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button1;

		private PrinterClass PrintC;
		private System.Windows.Forms.DataGrid dataGrid;

		private OleDbConnection conn = null;
		private string connectionString =
			"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\Northwind.mdb";

		private string sql = null;
        private DataSet ds = null;
        public string headerInfo = "Masom Reciept Book Information";
		

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form2(ref DataSet dataSet)
		{
			//
			// Required for Windows Form Designer support
			//
            ds = dataSet;
			InitializeComponent();
		}
		private void Form1_Load(object sender,System.EventArgs e)
		{
			FillDataGrid();

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form2));
            this.dataGrid = new System.Windows.Forms.DataGrid();
            this.printDocument1 = new System.Drawing.Printing.PrintDocument();
            this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();
            this.button4 = new System.Windows.Forms.Button();
            this.printDocument2 = new System.Drawing.Printing.PrintDocument();
            this.printPreviewDialog2 = new System.Windows.Forms.PrintPreviewDialog();
            this.printDialog1 = new System.Windows.Forms.PrintDialog();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.button3 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button1 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid)).BeginInit();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // dataGrid
            // 
            this.dataGrid.AllowNavigation = false;
            this.dataGrid.AllowSorting = false;
            this.dataGrid.BackColor = System.Drawing.Color.White;
            this.dataGrid.CaptionBackColor = System.Drawing.SystemColors.AppWorkspace;
            this.dataGrid.DataMember = "";
            this.dataGrid.Font = new System.Drawing.Font("Times New Roman", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
            this.dataGrid.GridLineColor = System.Drawing.Color.Black;
            this.dataGrid.HeaderBackColor = System.Drawing.Color.White;
            this.dataGrid.HeaderFont = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
            this.dataGrid.HeaderForeColor = System.Drawing.Color.Black;
            this.dataGrid.Location = new System.Drawing.Point(0, 0);
            this.dataGrid.Name = "dataGrid";
            this.dataGrid.Size = new System.Drawing.Size(592, 200);
            this.dataGrid.TabIndex = 0;
            this.dataGrid.Navigate += new System.Windows.Forms.NavigateEventHandler(this.dataGrid_Navigate);
            // 
            // printDocument1
            // 
            this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
            // 
            // printPreviewDialog1
            // 
            this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
            this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
            this.printPreviewDialog1.ClientSize = new System.Drawing.Size(400, 300);
            this.printPreviewDialog1.Document = this.printDocument1;
            this.printPreviewDialog1.Enabled = true;
            this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
            this.printPreviewDialog1.Name = "printPreviewDialog1";
            this.printPreviewDialog1.Visible = false;
            // 
            // button4
            // 
            this.button4.Location = new System.Drawing.Point(384, 216);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(104, 48);
            this.button4.TabIndex = 4;
            this.button4.Text = "Print with header and footer";
            this.button4.Visible = false;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // printDocument2
            // 
            this.printDocument2.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument2_PrintPage);
            // 
            // printPreviewDialog2
            // 
            this.printPreviewDialog2.AutoScrollMargin = new System.Drawing.Size(0, 0);
            this.printPreviewDialog2.AutoScrollMinSize = new System.Drawing.Size(0, 0);
            this.printPreviewDialog2.ClientSize = new System.Drawing.Size(400, 300);
            this.printPreviewDialog2.Document = this.printDocument2;
            this.printPreviewDialog2.Enabled = true;
            this.printPreviewDialog2.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog2.Icon")));
            this.printPreviewDialog2.Name = "printPreviewDialog2";
            this.printPreviewDialog2.Visible = false;
            // 
            // printDialog1
            // 
            this.printDialog1.AllowSelection = true;
            this.printDialog1.AllowSomePages = true;
            this.printDialog1.Document = this.printDocument1;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.button3);
            this.groupBox1.Controls.Add(this.button2);
            this.groupBox1.Controls.Add(this.button1);
            this.groupBox1.Location = new System.Drawing.Point(56, 224);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(224, 80);
            this.groupBox1.TabIndex = 6;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Print without header";
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(8, 32);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 8;
            this.button3.Text = "PrintDialog";
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(96, 48);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(120, 23);
            this.button2.TabIndex = 7;
            this.button2.Text = "Preview";
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(96, 24);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(120, 23);
            this.button1.TabIndex = 6;
            this.button1.Text = "Print";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form2
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(624, 331);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.dataGrid);
            this.Name = "Form2";
            this.Text = "Print";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid)).EndInit();
            this.groupBox1.ResumeLayout(false);
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		/*static void Main() 
		{
			Application.Run(new Form1());
		}*/

		private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
		{
			//print grid
			bool morepages = PrintC.Print(e.Graphics);
			if (morepages)
			{
				e.HasMorePages = true;
			}
			
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			//create printerclass object
			PrintC = new PrinterClass(printDocument1,dataGrid);

			printDocument1.Print();
		}

		private void FillDataGrid()
		{
			try
			{
				/*sql = "SELECT CustomerID,CompanyName,ContactName,Address,City,Country,Phone,Fax FROM Customers";
				conn = new OleDbConnection(connectionString);
				OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
				ds = new DataSet("Customers");
				adapter.Fill(ds, "Customers");*/
				dataGrid.DataSource = ds.Tables[0].DefaultView;
				// By default there is no DataGridTableStyle object.
				// Add all DataSet table's style to the DataGrid
				/*foreach(DataTable dTable in ds.Tables)
				{
					DataGridTableStyle dgStyle = new DataGridTableStyle();
					dgStyle.MappingName = dTable.TableName;
					dataGrid.TableStyles.Add(dgStyle);
				}*/
				// DataGrid settings
                dataGrid.CaptionText = headerInfo;
				dataGrid.HeaderFont = new Font("Verdana", 12);
                dataGrid.ReadOnly = true;
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
			}
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			//create printerclass object
			PrintC = new PrinterClass(printDocument1,dataGrid);

			//preview
			if (printPreviewDialog1.ShowDialog() == DialogResult.Cancel){}

			
			
		}

		private void printDocument2_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
		{
			float CurrentX = (e.PageBounds.Width - e.MarginBounds.Left) / 2; //center
			float CurrentY = e.MarginBounds.Top;

            //header
			string text = dataGrid.CaptionText;
			Font font = dataGrid.CaptionFont;
			RectangleF rect = new RectangleF(CurrentX,CurrentY,e.Graphics.MeasureString(text,font).Width,e.Graphics.MeasureString(text,font).Height);
			e.Graphics.FillRectangle(new SolidBrush(dataGrid.CaptionBackColor),rect);
			e.Graphics.DrawString(text,font,Brushes.Black,rect);
			
			CurrentY = CurrentY + rect.Height;

			//print with given X and Y start
			bool morepages = PrintC.Print(e.Graphics,ref CurrentX,ref CurrentY);
			if (morepages)
			{
				e.HasMorePages = true;
			}

			//footer
			CurrentX = (e.PageBounds.Width - e.MarginBounds.Left) / 2; //center
			CurrentY += 20;
			text = PrintC.PageCounter.ToString();
			rect = new RectangleF(CurrentX,CurrentY,e.Graphics.MeasureString(text,font).Width,e.Graphics.MeasureString(text,font).Height);
			e.Graphics.FillRectangle(new SolidBrush(dataGrid.CaptionBackColor),rect);
			e.Graphics.DrawString(text,font,Brushes.Black,rect);


		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			//create printerclass object
			PrintC = new PrinterClass(printDocument2,dataGrid);

			//preview with header and footer
			printPreviewDialog2.ShowDialog();
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			printDialog1.ShowDialog();
		}

        private void dataGrid_Navigate(object sender, NavigateEventArgs ne)
        {

        }	
			
	}
}
